﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1.Theme
{
    public class DarkColorTable : ProfessionalColorTable
    {
        // 主界面背景色
        public override Color ToolStripDropDownBackground => Color.FromArgb(45, 45, 48);
        // 菜单项背景色
        public override Color MenuItemSelected => Color.FromArgb(62, 62, 64);
        public override Color MenuItemBorder => Color.FromArgb(62, 62, 64);
        public override Color MenuItemSelectedGradientBegin => Color.FromArgb(62, 62, 64);
        public override Color MenuItemSelectedGradientEnd => Color.FromArgb(62, 62, 64);
        // 菜单顶部背景
        public override Color MenuStripGradientBegin => Color.FromArgb(37, 37, 38);
        public override Color MenuStripGradientEnd => Color.FromArgb(37, 37, 38);
        // 菜单边框颜色
        public override Color MenuBorder => Color.FromArgb(63, 63, 70);
        // 图像边距区域
        public override Color ImageMarginGradientBegin => Color.FromArgb(56, 56, 57);
        public override Color ImageMarginGradientMiddle => Color.FromArgb(56, 56, 57);
        public override Color ImageMarginGradientEnd => Color.FromArgb(56, 56, 57);
        public override Color ImageMarginRevealedGradientBegin => Color.FromArgb(56, 56, 57);
        public override Color ImageMarginRevealedGradientMiddle => Color.FromArgb(56, 56, 57);
        public override Color ImageMarginRevealedGradientEnd => Color.FromArgb(56, 56, 57);
        // 分隔符颜色
        public override Color SeparatorDark => Color.FromArgb(69, 69, 73);
        public override Color SeparatorLight => Color.FromArgb(69, 69, 73);
    }
    public class DarkMenuRenderer : ToolStripProfessionalRenderer
    {
        private readonly Font _menuFont = new Font("Segoe UI", 9);
        public DarkMenuRenderer() : base(new DarkColorTable())
        {
            RoundedEdges = false;
        }

        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            if (e.ToolStrip is MenuStrip)
            {
                // 处理顶层菜单激活状态（展开子菜单时）
                if (e.Item.Pressed || e.Item.Selected)
                {
                    var rect = new Rectangle(0, 0, e.Item.Width - 2, e.Item.Height);
                    using (var brush = new SolidBrush(ColorTable.MenuItemSelected))
                    {
                        e.Graphics.FillRectangle(brush, rect);
                    }
                    using (var pen = new Pen(ColorTable.MenuBorder))
                    {
                        e.Graphics.DrawRectangle(pen, rect.Left, rect.Top, rect.Width, rect.Height);
                    }
                }
                else
                {
                    // 普通状态使用默认渲染
                    base.OnRenderMenuItemBackground(e);
                }
            }
            else
            {
                // 处理下拉菜单项的背景
                base.OnRenderMenuItemBackground(e);
            }
        }
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            // 设置文字颜色
            if (!e.Item.Enabled)
                e.TextColor = Color.FromArgb(153, 153, 153);
            else
                e.TextColor = e.Item.Selected ? Color.White : Color.FromArgb(241, 241, 241);
            base.OnRenderItemText(e);
        }
        protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
        {
            // 设置箭头颜色
            e.ArrowColor = e.Item.Enabled ?
                (e.Item.Selected ? Color.White : Color.FromArgb(200, 200, 200)) :
                Color.FromArgb(153, 153, 153);
            base.OnRenderArrow(e);
        }
        protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
        {
            // 自定义分隔线渲染
            var pen = new Pen(ColorTable.SeparatorDark, 1);
            var rect = new Rectangle(28, 3, e.Item.Width - 32, 1);
            e.Graphics.DrawLine(pen, rect.Left, rect.Top, rect.Right, rect.Top);
        }
    }
}
